<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="bbs.*,core.*,util.*, java.util.*" %>
<%@ include file="/WEB-INF/include/checkUser.jsp" %>
<!DOCTYPE html>
<html>
<head>
<%@ include file="/WEB-INF/include/header.jsp" %>
<style>
    body table.table { width: 500px; margin-bottom: 4px; }
    input.span3 { margin: 0; }
    td:nth-child(2), td:nth-child(4) { width: 50px; }
    td:nth-child(4), th:nth-child(4) { text-align: right; }
</style>
<script>
    $(function() {
        if (window.parent) {
            var h = $("html").height();
            $('iframe[src*=boardEdit2]', window.parent.document).css("height", h);
        }
    });
</script>
</head>
<%
    String errMsg = null,infoMsg = null;

    RequestParameter param = new RequestParameter(request);
    String cmd = param.getString("cmd", "");
    int boardId = param.getInt("bid", 0);
    BoardService boardService = new BoardService(request, boardId);
    if (boardService.canManageBoard() == false)
        new UnauthorizedAccessException("게시판 관리 권한 없음");

    if (request.getMethod().equals("POST")) {
        if (cmd.equals("insert")) {
            String name = param.getString("name", "");
            if (name.length() >= 2) {
                ArticleCategoryDAO.insert(boardId, name);
                infoMsg = "저장되었습니다";
            } else errMsg = "제목이 너무 짧습니다";
        }
        else if (cmd.equals("delete")) {
            int[] idList = param.getIntValues("did");
            ArticleCategoryDAO.delete(idList);
            infoMsg = "삭제되었습니다";
        }
    }
    ArrayList<ArticleCategory> categoryList = ArticleCategoryDAO.selectAll(boardId);
%>
<body>
    <h3>게시글 분류</h3>
    <form method="post">
        <table class="table table-condensed table-bordered">
            <thead>
                <tr>
                    <td></td>
                    <th>ID</th>
                    <th>제목</th>
                    <th>글수</th>
                </tr>
            </thead>
            <tbody>
                <% for (ArticleCategory category : categoryList) { %>
                    <tr>
                        <td><input type="checkbox" name="did" value="<%=category.getId()%>"
                                   <%= category.getCount() > 0 ? "disabled" : "" %>>
                        </td>
                        <td><%= category.getId() %></td>
                        <td><%= category.getName() %></td>
                        <td><%= category.getCount() %></td>
                    </tr>
                <% } %>
                <tr>
                    <td></td><td></td>
                    <td colspan="2"><input type="text" class="span3" name="name" />
                        <button type="submit" class="btn btn-small" name="cmd" value="insert">
                            <i class="icon-plus"></i> 항목 추가
                        </button>
                    </td>
                <tr>
            </tbody>
        </table>
        <button type="submit" class="btn" name="cmd" value="delete">
            <i class="icon-remove"></i> 선택항목 삭제
        </button>
    </form>

    <% if (errMsg != null) { %>
        <div class="alert alert-error"><%= errMsg %></div>
    <% } %>
    <% if (infoMsg != null) { %>
        <div class="alert alert-info"><%= infoMsg %></div>
    <% } %>
</body>
</html>
